package aipay.mobile.SQLite;

import java.util.ArrayList;
import java.util.List;

import aipay.mobile.model.Acount;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper {

	
	private static final int DATABASE_VERSION = 1;
	
	private static final String DATABASE_NAME ="AcountManager";
	
	private static final String TABLE_ACOUNT ="Acount";
	
	private static final String KEY_ID ="id";
	private static final String KEY_USER="user";
	private static final String KEY_CHECK_SAVE_ID ="check_save_id";
	private static final String KEY_REGISTER ="register"; 
	private static final String KEY_NAME="name";
	private static final String KEY_PASS="pass";
	private static final String KEY_PASS_PORT= "pass_port";
	
	public DatabaseHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {

		String CREATE_TABLE_ACOUNT = "CREATE TABLE " + TABLE_ACOUNT + " ("+KEY_ID +" INTEGER PRIMARY KEY,"
		+ KEY_USER+" TEXT,"
		+ KEY_CHECK_SAVE_ID+" TEXT,"
		+ KEY_REGISTER +" TEXT,"
		+ KEY_NAME + " TEXT,"
		+ KEY_PASS + " TEXT,"
		+ KEY_PASS_PORT + " TEXT"+ ")";
		db.execSQL(CREATE_TABLE_ACOUNT);
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

		db.execSQL("DROP TABLE IF EXISTS " + TABLE_ACOUNT);
		onCreate(db);
	}
	
	// thêm dữ liệu
	public void addAcount(Acount acount) {
		SQLiteDatabase db = this.getWritableDatabase();
 
		ContentValues values = new ContentValues();
		values.put(KEY_USER,acount.getUser()); // Id User
		values.put(KEY_CHECK_SAVE_ID,acount.getCheck()); 
		values.put(KEY_REGISTER,acount.getRegistter());
		values.put(KEY_NAME,acount.getName());
		values.put(KEY_PASS,acount.getPass());
		values.put(KEY_PASS_PORT,acount.getPassport());
		// Inserting Row
		db.insert(TABLE_ACOUNT, null, values);
		db.close(); // Closing database connection
	}

	
	
	//lấy dữ liệu trên 1 dòng theo id
	
	public Acount getAcount(int ID) {
		SQLiteDatabase db = this.getReadableDatabase();
 
		Cursor cursor = db.query(TABLE_ACOUNT,null, KEY_ID + "=?",new String[] { String.valueOf(ID) }, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
 
		Acount acount = new Acount(cursor.getString(1),Integer.parseInt(cursor.getString(2)),Integer.parseInt(cursor.getString(3)),
				cursor.getString(4),cursor.getString(5),cursor.getString(6));
		return acount;
	}
	
	
	// lấy tất cả thông tin
	// Getting All Acount
	 public List<Acount> getAllAcount() {
	    List<Acount> acounttList = new ArrayList<Acount>();
	    // Select All Query
	    String selectQuery = "SELECT  * FROM " + TABLE_ACOUNT;
	 
	    SQLiteDatabase db = this.getReadableDatabase();
	    Cursor cursor = db.rawQuery(selectQuery, null);
	 
	    // looping through all rows and adding to list
	    if (cursor.moveToFirst()) {
	        do {
	            Acount acount = new Acount();
	            acount.setId(Integer.parseInt(cursor.getString(0)));
	            acount.setUser(cursor.getString(1));
	            acount.setCheck(Integer.parseInt(cursor.getString(2)));
	            acount.setRegistter(Integer.parseInt(cursor.getString(3)));
	            acount.setName(cursor.getString(4));
	            acount.setPass(cursor.getString(5));
	            acount.setPassport(cursor.getString(6));
	            // Adding contact to list
	            acounttList.add(acount);
	        } while (cursor.moveToNext());
	    }
	 
	    // return contact list
	    return acounttList;
	}
	

	//lấy tổng số thông tin
	public int getAcountCount() {
		String countQuery = "SELECT  * FROM " + TABLE_ACOUNT;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		return cursor.getCount();
	}
	
	
	//cập nhật 1 số thông tin
	public int updateAcount(Acount acount) {
		SQLiteDatabase db = this.getWritableDatabase();
 
		ContentValues values = new ContentValues();
//		values.put(KEY_USER, acount.getUser());
		values.put(KEY_CHECK_SAVE_ID,acount.getCheck());
		values.put(KEY_REGISTER,acount.getRegistter());
		values.put(KEY_NAME,acount.getName());
		values.put(KEY_PASS,acount.getPass());
		values.put(KEY_PASS_PORT,acount.getPassport());
		
		return db.update(TABLE_ACOUNT, values, KEY_USER + " = ?",
		new String[] { String.valueOf(acount.getUser()) });
	}
	
	//xóa 1 thông tin
	
	public void deleteAcount(Acount acoount) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_ACOUNT, KEY_ID + " = ?",
				new String[] { String.valueOf(acoount.getId()) });
		db.close();
	}
	
	// check status register 
	public Acount CheckRegister(){
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.query(TABLE_ACOUNT,null, KEY_REGISTER + "=?",new String[] { String.valueOf(1) }, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		Acount acount = new Acount(cursor.getString(1),Integer.parseInt(cursor.getString(2)),
				Integer.parseInt(cursor.getString(3)),cursor.getString(4),cursor.getString(5),cursor.getString(6));
		return acount;
	}
	
}




